package com.springbootmybatiscurd.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.springbootmybatiscurd.mapper.emp.empMapper;
import com.springbootmybatiscurd.mapper.user.userLogin;
import com.springbootmybatiscurd.pojo.*;
import com.springbootmybatiscurd.service.empService;
import com.springbootmybatiscurd.service.userLoginService;
import com.springbootmybatiscurd.utils.uuid.getMachineUUID;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class empServiceImpl implements empService , userLoginService{

    /**
     查询获取员工数据

     * */
    @Autowired
    private empMapper empMapper;
   /**
    * 员工查询
    * */
    @Override
    public PageBean listEmpService(Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Emp> empList = empMapper.listEmp();
        Page<Emp> p = (Page<Emp>) empList;
        // 封装
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());

        return pageBean;
    }
    /**
     * 分页模糊匹配查询
     * */
    @Override
    public PageBean pageService(Integer page,Integer pageSize, @Param("name") String name, @Param("gender")
    short gender, @Param("startTime") LocalDate startTime, @Param("endTime") LocalDate endTime) {

        // 封装
        PageHelper.startPage(page,pageSize);
        List<Emp> emps = empMapper.queryPage(name, gender, startTime, endTime);

        Page<Emp> p = (Page<Emp>) emps;
        System.out.println(p+"---------------");
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;

       /* List<Emp> pageList = empMapper.page((page-1)*pageSize, pageSize);
        // 总记录数
        long count = empMapper.count();
        PageBean pageBean=new PageBean(count,pageList);*/

            //log.info("{},{},{},{},{},{}",page, pageSize,name,gender,startTime,endTime);

            // 第二种方法  使用分页插件  PageHelper
      /*  PageHelper.startPage(page,pageSize);
        List<Emp> empList = empMapper.queryPage(name,  gender, startTime, endTime);
        System.out.println(empList);*/
       /*  List<Emp> empList = empMapper.queryPage(name,  gender, startTime, endTime);
        System.out.println(empList);
          Page<Emp> p = (Page<Emp>) empList;*/
            // 封装
            //  PageBean pageBean = new PageBean(p.getTotal(), p.getResult());




        }



    /**
     * 增加员工
     * */

    @Override
    public String addEmpService(Emp emp) {
        // 查找有无重复的用户名和名称
        List<Emp> list = empMapper.listEmp();


        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(emp.getUsername()) ||list.get(i).getName().equals(emp.getName()) ){
                 return "error";

            }
        }
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        empMapper.addEmpDao(emp);
            return "OK" ;
      }

      /**
       * 删除员工
       * */

      public Integer deleteEmpService(Integer id){
          int index = empMapper.deleteEmp(id);
          return index;
      }
    /**
     * 修改员工
     * */

    @Override
    public Emp updateEmpService(Emp emp) {

        emp.setUpdateTime(LocalDateTime.now());
       // System.out.println(emp);
        Emp updateEmp = empMapper.updateEmp(emp);
        return updateEmp;
    }



    /**
        * 登录
        * */
      @Autowired
      private userLogin userLogin;
      @Override
      public Result userLogin(String username, String password) {
          User user = userLogin.userLogin(username, password);
          HashMap<String, String> hash = new HashMap<>();
          String machineUUID = new getMachineUUID().getUUID();

          hash.put("token", machineUUID);

          hash.put("userId", "chu");

          if(user==null){

              return new Result(100, "error", "用户名或密码错误");
          }else {
              return new Result(200, "success", hash);
          }

      }


      /**
       * 查询员工
       * */
    /*  @Override
      public List<Query> queryListService(Query query) {
          System.out.println(query);
          LocalDate begin = null;
          LocalDate end = null;
          if(query.getBegin()!=null ||query.getEnd()!=null){
              String start1 = query.getBegin().split("T")[0];
              String end1 = query.getEnd().split("T")[0];
               begin = LocalDate.parse(start1, DateTimeFormatter.ISO_DATE);
               end = LocalDate.parse(end1, DateTimeFormatter.ISO_DATE);
          }


          List<Query> emps = empMapper.queryEmp(query.getName(), query.getGender(), begin, end);
            return emps;
      }*/


      /**
       * 批量删除员工数据
       * */
    @Override
    public List<Integer> deleteAllService(List<Integer> list) {

          empMapper.deleteByIds(list);
        return list;
    }
    /**
     * 查询回显数据
     * */
    @Override
    public Emp getByIdService(Integer id) {
      Emp getByIdEmp=empMapper.getByIdEmp(id);
      return getByIdEmp;
    }


}
